/**
	exec SP_SALES_FORECAST_SNAPSHOT_REPORT 'Jun_2010'
*/
CREATE PROC SP_SALES_FORECAST_SNAPSHOT_REPORT @SNAPSHOT_NAME VARCHAR(50) 
AS
BEGIN
	
	DECLARE @USERLIST1_LABEL VARCHAR(50)
	DECLARE @USERLIST2_LABEL VARCHAR(50)
	DECLARE @QUERY_STR NVARCHAR(2000)
	DECLARE @YEAR_TO INT
	DECLARE @YEAR_FROM INT
	DECLARE @MONTH_FROM INT
	DECLARE @MONTH_TO INT
	DECLARE @SHOW_NEXT INT
	DECLARE @TMP_DATE DATETIME

	SET @YEAR_FROM = YEAR(CURRENT_TIMESTAMP)
	SET @MONTH_FROM = MONTH(CURRENT_TIMESTAMP)-1
	SET @SHOW_NEXT = 24
		SELECT @USERLIST1_LABEL = USERLIST1, @USERLIST2_LABEL = USERLIST2 FROM PROJSETUP WHERE PROJID = (SELECT MAX(PROJID) FROM PROJSETUP)
		SET @QUERY_STR = N' --INSERT INTO #RESULTS
			SELECT C.Customer_ID,C.C_NAME,
				EC.C_NAME AS END_CUSTOMER,				
				SP.SALESPERSON_NAME, 
				PS.PROJNAME, P.ProcessStep_ID, P.P_Parent_Product_ID,
				P.P_MARKETING_PART_NUMBER, SP.SalesPerson_ID,
				USERLIST1VAL,
				USERLIST2VAL,				
				MONTH,
				YEAR,
				QTY,
				PRICE, --select * from FCENTRYMASTER where  price> 0
				SNAPSHOT_NAME
			FROM PROJSETUP PS, CUSTOMER C, PRODUCT P, SALES_PERSONS SP, FORECAST_SNAPSHOT_TABLE FCST
			LEFT OUTER JOIN CUSTOMER EC ON EC.CUSTOMER_ID = FCST.CUSTID
			WHERE FCST.PROJID = PS.PROJID
				AND C.CUSTOMER_ID = PS.CUSTID
				AND P.PRODUCT_ID = FCST.PRODID
				AND SP.SALESPERSON_ID = PS.SPERSONID
				AND PS.PROJDISABLED = 1 
				AND FCST.SNAPSHOT_NAME = ''' + @SNAPSHOT_NAME + ''''
		
			SET @QUERY_STR = @QUERY_STR + ' ORDER BY SP.SALESPERSON_NAME, P.P_MARKETING_PART_NUMBER, YEAR, MONTH'		
	PRINT @QUERY_STR
	EXEC (@QUERY_STR)	
	
END